package org.zero;

import lombok.SneakyThrows;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Properties;

/**
 * @author Zero
 */
public class PropertiesEnhancer {
    private static final String XML = "xml";

    private final Properties properties;

    public PropertiesEnhancer() {
        properties = new Properties();
    }

    public PropertiesEnhancer(Properties properties) {
        this.properties = properties;
    }

    public PropertiesEnhancer(String filePath) {
        this();
        load(filePath);
    }

    public PropertiesEnhancer(File file) {
        this();
        load(file);
    }

    public void load(String filePath) {
        InputStream inputStream = PropertiesEnhancer.class.getClassLoader().getResourceAsStream(filePath);
        if (filePath.endsWith(XML)) {
            loadFromXml(inputStream);
        } else {
            loadFromFile(inputStream);
        }
    }

    @SneakyThrows
    public void load(File file) {
        InputStream inputStream = Files.newInputStream(file.toPath());
        if (file.getAbsolutePath().endsWith(XML)) {
            loadFromXml(inputStream);
        } else {
            loadFromFile(inputStream);
        }
    }

    @SneakyThrows
    public void loadFromFile(InputStream inputStream) {
        properties.load(inputStream);
    }

    @SneakyThrows
    public void loadFromXml(InputStream inputStream) {
        properties.loadFromXML(inputStream);
    }

    public String getString(String key) {
        return properties.getProperty(key);
    }

    public String getString(String key, String defaultValue) {
        return properties.getProperty(key, defaultValue);
    }

    public byte getByte(String key) {
        return Byte.parseByte(properties.getProperty(key));
    }

    public byte getByte(String key, byte defaultValue) {
        return Byte.parseByte(properties.getProperty(key, Byte.toString(defaultValue)));
    }

    public short getShort(String key) {
        return Short.parseShort(properties.getProperty(key));
    }

    public short getShort(String key, short defaultValue) {
        return Short.parseShort(properties.getProperty(key, Short.toString(defaultValue)));
    }

    public int getInt(String key) {
        return Integer.parseInt(properties.getProperty(key));
    }

    public int getInt(String key, int defaultValue) {
        return Integer.parseInt(properties.getProperty(key, Integer.toString(defaultValue)));
    }

    public long getLong(String key) {
        return Long.parseLong(properties.getProperty(key));
    }

    public long getLong(String key, long defaultValue) {
        return Long.parseLong(properties.getProperty(key, Long.toString(defaultValue)));
    }

    public float getFloat(String key) {
        return Float.parseFloat(properties.getProperty(key));
    }

    public float getFloat(String key, float defaultValue) {
        return Float.parseFloat(properties.getProperty(key, Float.toString(defaultValue)));
    }

    public double getDouble(String key) {
        return Double.parseDouble(properties.getProperty(key));
    }

    public double getDouble(String key, double defaultValue) {
        return Double.parseDouble(properties.getProperty(key, Double.toString(defaultValue)));
    }

    public boolean getBoolean(String key) {
        return Boolean.parseBoolean(properties.getProperty(key));
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        return Boolean.parseBoolean(properties.getProperty(key, Boolean.toString(defaultValue)));
    }
}

